package com.four.dailymanage.dish.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.four.dailymanage.common.bean.Page;
import com.four.dailymanage.common.bean.ResponseData;
import com.four.dailymanage.common.controller.BaseController;
import com.four.dailymanage.dish.entity.PayTotal;
import com.four.dailymanage.dish.service.PayTotalService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/payTotal")
public class PayTotalController extends BaseController {
    @Autowired
    PayTotalService payTotalService;
    @GetMapping("/getPayTotal")
    public ResponseData<Object> getPayTotal(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "startDate",required = false) String startDate,
            @RequestParam(value = "endDate",required = false)String endDate,
            @RequestParam(value = "accountName",required = false)String accountName
    ){
        return returnObject(payTotalService.getPayTotalDish(pageNum, pageSize, startDate, endDate, accountName));
    }

    @GetMapping("/export")
    public void exportData(@RequestParam(value = "startDate",required = false) String startDate,
                           @RequestParam(value = "endDate",required = false)String endDate,
                           @RequestParam(value = "accountName",required = false)String accountName,
                           @RequestParam(value = "id",required = false) String ids,
                           HttpServletResponse response) throws IOException {

        ExcelWriter writer= ExcelUtil.getWriter(true);


        List<PayTotal> list=new ArrayList<>();
        if (StrUtil.isNotBlank(ids)){
            //选择行的导出 ["1","2","3"]->[1,2,3]
             List<Integer> idsArr = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
//             System.out.println(idsArr);
             list=payTotalService.payRowsTotal(idsArr);
        }else {
            //第一种全部导出(有条件+无条件)
            list=payTotalService.payTotalList(startDate, endDate, accountName);
        }
        writer.write(list,true);


        //设置浏览器的响应格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName= URLEncoder.encode("充值信息汇总表","UTF-8");
        //以附件的形式下载
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");

        ServletOutputStream outputStream=response.getOutputStream();
        writer.flush(outputStream,true);
        writer.close();
        outputStream.flush();
        outputStream.close();
    }
}
